/* cl.ext.datetime 0.1*/
(
    function (factory) {
        if (typeof define === 'function' && define.amd) {
            define(['jquery',"moment","DatePicker"], function($,momentjs) {
               factory($,momentjs);
            });
        } else if (typeof module === 'object' && module.exports) {
            var momentjs = require('moment');
            var pickerjs = require('DatePicker');
            //var langjs = require('DatePickerLang');
            module.exports = function( root, jQuery ) {
                if ( jQuery === undefined ) {
                    if ( typeof window !== 'undefined' ) {
                        jQuery = require('jquery');
                    }
                    else {
                        jQuery = require('jquery')(root);
                    }
                }
                factory(jQuery,momentjs);
                //return jQuery;
            };
        } else {
            factory(jQuery, moment);
        }
    }(
    function ($,moment){
        var defaultExtInfo={type:"date",initEmpty:true,changeCallback:null}; //type: date|dateStartEnd
        var defaults={infoExt:defaultExtInfo};
        var $modalCommon={
            getDefaults :function () {
                return defaults
            },
            getOptions : function (elementData,options) {
                options=$.ExtCommon.isCheckEmpty(options)?{}:options;
                var optionBase=$.ExtCommon.initMergeJson(elementData,options);
                optionBase =$.ExtCommon.initMergeJson(this.getDefaults(),optionBase);
                return optionBase
            },
            getOptionsData:function(options){
                var tempOption={format:"yyyy-mm-dd",language: "zh-CN",autoclose:true};
                return $.ExtCommon.initMergeJson(tempOption,options);
            }
        };
        var DateTimeExtModal = function(element, options) {
            var self = this;
            self.element = $(element);
            self.optionBase = $modalCommon.getOptions(self.element.data(), options);
            switch (self.optionBase.infoExt.type) {
                case "date":
                    self.initDatePicker( self.element,self.optionBase);
                    break;
                case "dateStartEnd":
                    self.initDateStartEnd();
                    break;
            }
        };
        DateTimeExtModal.prototype = {
            initDatePicker: function (element, options) {
                var self = this;
                var tempObj = null;
                var tempOption = {};
                if (element) {
                    tempObj = element;
                    tempOption = $modalCommon.getOptions({}, options);
                    tempOption = $modalCommon.getOptionsData(tempOption);
                } else {
                    tempObj = self.element;
                    tempOption = self.optionBase;
                }
                if (tempObj.val().length == 0 && tempOption.infoExt.initEmpty)tempObj.val($.ExtMoment.initFormat());
                tempObj.datepicker(tempOption);
                if (tempOption.infoExt.changeCallback) {
                    tempObj.off("changeDate").on("changeDate", function (ev) {
                        tempOption.infoExt.changeCallback(ev)
                    });
                }
                tempObj.parent().find("i").off("click").on("click",function() {
                    tempObj.focus()
                });
            },
            initDateStartEnd: function () {
                var self = this;
                var infoExt = self.optionBase.infoExt;
                var objStartTime = self.element;
                var objEndTime = infoExt.objEndTime;
                if (!$.ExtCommon.isCheckEmpty(objStartTime) && !$.ExtCommon.isCheckEmpty(objEndTime)) {
                    var valueStartTime =$.ExtMoment.initFormat(objStartTime.val());
                    var valueEndTime = $.ExtMoment.initFormat(objEndTime.val());
                    objStartTime.val(valueStartTime);
                    objEndTime.val(valueEndTime);
                    if ($.ExtCommon.isCheckEmpty(infoExt.isInitStart)) infoExt.isInitStart = true;
                    objStartTime.prop("disabled",false).off("changeDate").datepicker("destroy");
                    if (infoExt.isInitStart) {
                        if (valueStartTime.length == 0) {
                            valueStartTime = $.ExtMoment.initFormat();
                            objStartTime.val(valueStartTime);
                        }
                        var optionStart = {
                            infoExt: {
                                changeCallback: function (ev) {
                                    objEndTime.datepicker('setStartDate', ev.date);
                                    objEndTime.focus();
                                    if (infoExt.onChangStartTime)infoExt.onChangStartTime();
                                }
                            }
                        };
                        var isStartSetStartDate=$.ExtCommon.isCheckEmpty(infoExt.isStartSetStartDate)?true:infoExt.isStartSetStartDate;
                        if(isStartSetStartDate){optionStart.startDate=valueStartTime.length > 0 && moment(valueStartTime).isBefore(new Date()) ? valueStartTime : new Date()}
                        self.initDatePicker(objStartTime, optionStart);
                    }else{
                        objStartTime.prop("disabled",true)
                    }
                    if ($.ExtCommon.isCheckEmpty(infoExt.isInitEnd)) infoExt.isInitEnd = true;
                    objEndTime.prop("disabled",false).off("changeDate").datepicker("destroy");
                    if (infoExt.isInitEnd) {
                        if (valueEndTime.length == 0) {
                            valueEndTime = self.initAdd(1, "months");
                            objEndTime.val(valueEndTime);
                        }
                        var optionEnd = {
                            startDate: valueEndTime.length > 0 && moment(valueEndTime).isBefore(new Date()) ? valueEndTime : new Date(),
                            infoExt: {
                                changeCallback: function (ev) {
                                    if (infoExt.onChangEndTime)infoExt.onChangEndTime();
                                }
                            }
                        };
                        var isEndSetStartDate=$.ExtCommon.isCheckEmpty(infoExt.isEndSetStartDate)?true:infoExt.isEndSetStartDate;
                        if(isEndSetStartDate){
                            optionEnd.startDate= valueEndTime.length > 0 && moment(valueEndTime).isBefore(new Date()) ? valueEndTime : new Date()
                        }else{
                            optionEnd.startDate=valueStartTime
                        }
                        self.initDatePicker(objEndTime, optionEnd);
                    }else{
                        objEndTime.prop("disabled",true)
                    }}

            },
            replaceOptions:function(options){
                var self = this;
                self.optionBase = $.ExtCommon.initMergeJson(self.optionBase,options);
                switch (self.optionBase.infoExt.type) {
                    case "date":
                        self.initDatePicker( self.element,self.optionBase);
                        break;
                    case "dateStartEnd":
                        self.initDateStartEnd();
                        break;
                }
            },
            destroy: function () {
            }
        };
        $.fn.ExtDateTime = function(options) {
            var dataFun;
            this.each(function() {
                var el = $(this);
                var tempData=el.data('ExtDateTime');
                if (tempData) {
                    if (typeof  options == "string") {
                        if (tempData[options]) {
                            dataFun =tempData[options]()
                        } else {
                            dataFun = tempData
                        }
                    } else {
                        dataFun = tempData
                    }
                } else {
                    dataFun = new DateTimeExtModal(el, options);
                    el.data('ExtDateTime', dataFun);
                }
            });
            return dataFun;
        };
        //!function(a){a.fn.datepicker.dates["zh-CN"]={days:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],daysShort:["周日","周一","周二","周三","周四","周五","周六"],daysMin:["日","一","二","三","四","五","六"],months:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],monthsShort:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],today:"今日",clear:"清除",format:"yyyy年mm月dd日",titleFormat:"yyyy年mm月",weekStart:1}}(jQuery);
        (function($){
            $.fn.datepicker.dates["zh-CN"]={
                days:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
                daysShort:["周日","周一","周二","周三","周四","周五","周六"],
                daysMin:["日","一","二","三","四","五","六"],
                months:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
                monthsShort:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],
                today:"今日",
                clear:"清除",
                format:"yyyy年mm月dd日",
                titleFormat:"yyyy年mm月",
                weekStart:1
            };
        })(jQuery);
        /*(function(){
            var e = $.fn.select2.amd;
            return e.define("select2/i18n/zh-CN", [], function() {
                return {
                    errorLoading: function() {
                        return "无法载入结果。"
                    },
                    inputTooLong: function(e) {
                        var t = e.input.length - e.maximum
                            , n = "请删除" + t + "个字符";
                        return n
                    },
                    inputTooShort: function(e) {
                        var t = e.minimum - e.input.length
                            , n = "请再输入至少" + t + "个字符";
                        return n
                    },
                    loadingMore: function() {
                        return "载入更多结果…"
                    },
                    maximumSelected: function(e) {
                        var t = "最多只能选择" + e.maximum + "个项目";
                        return t
                    },
                    noResults: function() {
                        return "未找到结果"
                    },
                    searching: function() {
                        return "搜索中…"
                    }
                }
            }),
            {
                define: e.define,
                require: e.require
            }
        })();*/
        return DateTimeExtModal;
    })
);